from django.db import models
from django.contrib.auth.models import User


class EmotionDiary(models.Model):
    """情绪日记表"""
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        verbose_name='用户',
        db_comment='关联用户ID'
    )
    emotion_type = models.CharField(
        max_length=50, 
        verbose_name='情绪类型',
        db_comment='主要情绪类型(快乐/怒/哀/惧/焦虑/抑郁等)'
    )
    emotion_intensity = models.IntegerField(
        verbose_name='情绪强度',
        db_comment='情绪强度等级1-10'
    )
    description = models.TextField(
        verbose_name='描述',
        db_comment='情绪详细描述'
    )
    tags = models.JSONField(
        null=True, 
        blank=True, 
        verbose_name='标签',
        db_comment='情绪标签JSON数组'
    )
    trigger_factors = models.JSONField(
        null=True, 
        blank=True, 
        verbose_name='触发因素',
        db_comment='情绪触发因素JSON(工作/学习/人际/睡眠等)'
    )
    coping_methods = models.TextField(
        null=True, 
        blank=True, 
        verbose_name='应对方式',
        db_comment='采取的缓解方式'
    )
    diary_date = models.DateField(
        verbose_name='日记日期',
        db_comment='情绪发生日期'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )
    updated_at = models.DateTimeField(
        auto_now=True, 
        verbose_name='更新时间',
        db_comment='记录更新时间'
    )

    class Meta:
        db_table = 'xinshi_emotion_diary'
        db_table_comment = '情绪日记表'
        verbose_name = '情绪日记'
        verbose_name_plural = '情绪日记'
        ordering = ['-diary_date', '-created_at']

    def __str__(self):
        return f'{self.user.username} - {self.diary_date} - {self.emotion_type}'


class EmotionTrend(models.Model):
    """情绪趋势分析表"""
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        verbose_name='用户',
        db_comment='关联用户ID'
    )
    period_type = models.CharField(
        max_length=20, 
        verbose_name='统计周期',
        db_comment='统计周期类型(week/month/year)'
    )
    period_start = models.DateField(
        verbose_name='周期开始',
        db_comment='统计周期开始日期'
    )
    period_end = models.DateField(
        verbose_name='周期结束',
        db_comment='统计周期结束日期'
    )
    emotion_statistics = models.JSONField(
        verbose_name='情绪统计',
        db_comment='各种情绪占比和频次统计JSON'
    )
    dominant_emotion = models.CharField(
        max_length=50, 
        verbose_name='主导情绪',
        db_comment='该时期主导情绪类型'
    )
    average_intensity = models.FloatField(
        verbose_name='平均强度',
        db_comment='该时期情绪平均强度'
    )
    trend_analysis = models.TextField(
        null=True, 
        blank=True, 
        verbose_name='趋势分析',
        db_comment='情绪变化趋势分析描述'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )

    class Meta:
        db_table = 'xinshi_emotion_trend'
        db_table_comment = '情绪趋势分析表'
        verbose_name = '情绪趋势'
        verbose_name_plural = '情绪趋势'
        ordering = ['-period_start']

    def __str__(self):
        return f'{self.user.username} - {self.period_type} - {self.period_start}'


class EmotionWarning(models.Model):
    """情绪预警记录表"""
    WARNING_LEVEL_CHOICES = [
        ('low', '低度'),
        ('medium', '中度'),
        ('high', '高度'),
        ('urgent', '紧急'),
    ]
    
    user = models.ForeignKey(
        User, 
        on_delete=models.CASCADE, 
        verbose_name='用户',
        db_comment='关联用户ID'
    )
    warning_type = models.CharField(
        max_length=50, 
        verbose_name='预警类型',
        db_comment='预警类型(持续抑郁/焦虑波动/情绪失控等)'
    )
    warning_level = models.CharField(
        max_length=20, 
        choices=WARNING_LEVEL_CHOICES, 
        verbose_name='预警级别',
        db_comment='预警严重程度级别'
    )
    description = models.TextField(
        verbose_name='预警描述',
        db_comment='预警具体情况描述'
    )
    trigger_data = models.JSONField(
        verbose_name='触发数据',
        db_comment='触发预警的数据JSON'
    )
    suggestions = models.TextField(
        null=True, 
        blank=True, 
        verbose_name='建议',
        db_comment='针对预警的干预建议'
    )
    is_handled = models.BooleanField(
        default=False, 
        verbose_name='是否处理',
        db_comment='预警是否已处理'
    )
    handled_at = models.DateTimeField(
        null=True, 
        blank=True, 
        verbose_name='处理时间',
        db_comment='预警处理时间'
    )
    created_at = models.DateTimeField(
        auto_now_add=True, 
        verbose_name='创建时间',
        db_comment='记录创建时间'
    )

    class Meta:
        db_table = 'xinshi_emotion_warning'
        db_table_comment = '情绪预警记录表'
        verbose_name = '情绪预警'
        verbose_name_plural = '情绪预警'
        ordering = ['-created_at']

    def __str__(self):
        return f'{self.user.username} - {self.warning_type} - {self.warning_level}'
